'''third party packages'''
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from dash.dependencies import Input, Output
from pandas.api.types import is_string_dtype
from pandas.api.types import is_numeric_dtype
import pandas as pd


'''my modules,'''
from myplot1c import * #包含了绘图相关的函数,几c对应处理输入几列的绘图
from myplot2c import *
from myplot3c import *

'''demo data'''
df = pd.read_csv(
    'https://gist.githubusercontent.com/chriddyp/' +
    '5d1ea79569ed194d432e56108a04d188/raw/' +
    'a9f9e8076b837d541398e999dcbac2b2826a81f8/'+
    'gdp-life-exp-2007.csv')
# df =pd.read_csv('./mushrooms.csv')
# df =pd.read_csv('./iris.csv')

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

# 背景
empty_figure={
            'data': [
            ],
            'layout': {
                'title':'Empty figure'
            }
}

# app的整体布局
app.layout = html.Div([
    html.Div([html.H1("Explore Dataframe")],
                 style={'textAlign': "center", "padding-bottom": "10", "padding-top": "10"}),
    dcc.Graph(
        id='main-graph',
        figure=empty_figure
    ),
    dcc.Dropdown(
    id='columns-dropdown',
    options=[
        {'label':lb, 'value':lb} for lb in df.columns
    ],
    multi=True,
    ),
])

# 设置选择列的功能
@app.callback(
Output('main-graph', 'figure'),
[Input('columns-dropdown', 'value')])
def show(columnsValue):
    is_str=is_string_dtype
    is_num=is_numeric_dtype
    if columnsValue is not None:
        if(len(columnsValue)==0):
            return empty_figure
        elif (len(columnsValue)==1):
            col1=columnsValue[0]
            if(is_str(df[col1])):#该列为字符串型,生成pie图,(为用字符或单词表示类型的列设计)
                return {'data':[column_pie(df,columnsValue[0])],'layout':{'title':col1}}
            if(is_num(df[col1])):#数值列,生成histogram
                return {'data': [column_histogram(df,columnsValue[0])],'layout':column_histogram_layout(col1)}
        elif(len(columnsValue)==2):
            col1,col2=columnsValue[0],columnsValue[1]
            if (is_str(df[col1]) and is_str(df[col2])):#两列都是字符型,生成bubble图
                return {'data':[str_str_bubble(df,col1, col2)],'layout':str_str_bubble_layout(col1,col2)}
            elif(is_str(df[col1]) and is_num(df[col2])):#字符和数值,以字符为横轴生成bubble,
                return {'data': [str_num_bubble(df,col1, col2)],'layout':str_num_bubble_layout(col1,col2)}
            elif(is_num(df[col1]) and is_str(df[col2])):
                return {'data': [str_num_bubble(df,col2, col1)],'layout':str_num_bubble_layout(col2,col1)}
            elif(is_num(df[col1]) and is_num(df[col2])):#都是数值,生成scatter图
                return {'data':[two_column_scatter(df,col1,col2)],'layout':two_column_scatter_layout(col1,col2)}
        elif(len(columnsValue)==3):#3列,生成3d-scatter图
            col1, col2,col3 = columnsValue[0], columnsValue[1],columnsValue[2]
            if(is_num(df[col3])):
                return {'data': [three_dim_scatter_z_num(df,col1, col2, col3)], 'layout':three_dim_scatter_layout(col1, col2, col3)}
            elif(is_str(df[col3])):
                return {'data': [three_dim_scatter_z_str(df,col1, col2, col3)],
                        'layout': three_dim_scatter_layout(col1, col2, col3)}
        else:
            return empty_figure
    else:
        return empty_figure

if __name__ == '__main__':
    app.run_server(debug=True)